﻿using CoreShop.Model;
using CoreShop.Model.UI;
using CoreShop.Services;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Policy;
using System.Windows.Forms;
using SqlSugar.Extensions;
using CoreShop.Library;
using static CoreShop.Library.GlobalEnumVars;

namespace CoreShop.App
{
    public partial class FrmUserCategoryEdit : UIForm
    {
        corecmsusercategory userCategory = new corecmsusercategory();
        string postStr;
        string retStr;
        public bool returnValue;

        public FrmUserCategoryEdit(corecmsusercategory _userCategory)
        {
            InitializeComponent();
            userCategory = _userCategory;
        }

        public FrmUserCategoryEdit()
        {
            InitializeComponent();
        }

        private bool CheckData1()
        {
            if (name.Text == null)
            {
                UIMessageBox.Show("请输入名称", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }
            if (name.Text == null)
            {
                UIMessageBox.Show("请输入编码", "提示", UIStyle.Green, UIMessageBoxButtons.OK);
                return false;
            }
            return true;
        }

        private void FrmEditInputs_Load(object sender, EventArgs e)
        {
            ControlsDataBinding.DataBind(userCategory, this, DataSourceUpdateMode.OnPropertyChanged);

            //#region 加载优惠类型
            //this.discountType.Items.Clear();
            //var type = EnumHelper.EnumToList<GlobalEnumVars.discountType>();
            //discountType.DataSource = type;
            //discountType.DisplayMember = "description";
            //discountType.ValueMember = "value";
            //this.discountType.SelectedValue =(int)GlobalEnumVars.discountType.retailPrice;
            //#endregion

            //isIntegral.Checked = userCategory.isIntegral;
            //isDeposit.Checked = userCategory.isDeposit;
        }

        private void DoCreate()
        {
            try
            {
                string msg = null;
                //corecmsusercategory add = new PosRemoteServices(AppConstVars.sUrl).DoCreateUserCategory(userCategory, AppConstVars.sysUser.ccessToken, ref msg);
                //if (add != null)
                //{
                //    if (new corecmsusercategoryServices().InsertAsync(add))
                //    {
                //        ShowSuccessTip("保存成功");
                //        this.Close();
                //    }
                //    else
                //    {
                //        UIMessageBox.Show("保存失败", "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
                //        return;
                //    }
                //}
                //else
                //{
                //    UIMessageBox.Show("保存失败" + msg, "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
                //    return;
                //}
            }
            catch (Exception)
            {

                throw;
            }
        }

        
        private void SetControlClear()
        {
            foreach (Control Ctrol in this.Controls)
            {
                if (Ctrol is Sunny.UI.UITextBox)
                {
                    Ctrol.Text = "";
                }
            }
            userCategory.id = 0;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (!CheckData1()) return;
                //userCategory.sharingCode = this.sharingCode.Text;
                userCategory.name = this.name.Text;
                //userCategory.discount = CommFunc.ConvertToDecimal(this.discount.Text) / 100;
                //userCategory.discountType = CommFunc.ConvertToInt32(discountType.SelectedValue);
                //userCategory.isDeposit = this.isIntegral.Checked;
                //userCategory.isIntegral = this.isIntegral.Checked;
                if (userCategory.id > 0)
                {
                    DoEdit();
                }
                else
                {
                    DoCreate();
                }
                SetControlClear();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DoEdit()
        {
            try
            {
                string msg = "";
                //corecmsusercategory update = new PosRemoteServices(AppConstVars.sUrl).DoCreateUserCategory(userCategory, AppConstVars.sysUser.ccessToken, ref msg);
                //if (update != null)
                //{
                //    bool t = new corecmsusercategoryServices().UpdateAsync(update);
                //    if (!t)
                //    {
                //        UIMessageBox.Show("保存失败", "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
                //        return;
                //    }
                //    ShowSuccessTip("保存成功");
                //    this.Close();
                //}
                //else
                //{
                //    UIMessageBox.Show("保存失败" + msg, "系统提示", UIStyle.Red, UIMessageBoxButtons.OK);
                //    return;
                //}
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}